package controler;

import model.Cistern;
import model.Criteria;
import model.Environment;
import model.EnvironmentRegulator;
import model.LevelSensor;
import model.Pounding;
import model.SensedValue;
import model.Sensor;
import model.Source;
import model.Valve;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// testEnvironmentSensor();
		Test.testEnvironmentSensorEnvironmentRegulatorAffected();
		// testSensorWithCisternSource();
		// testSensorWithCisternSourceValveAffected();
		// testSensorWithCisternSourceValveAffectedAndPounding();
		// testLevelSensorWithCisternSourceValveAffected();

	}

	private static void testSensorWithCisternSource() {
		Cistern tank1 = new Cistern(Double.valueOf(200), 1);
		tank1.addContent(Double.valueOf(150));
		Test.testSensor(tank1);
		tank1.addContent(Double.valueOf(25));
		Test.testSensor(tank1);
		tank1.addContent(Double.valueOf(150));
		Test.testSensor(tank1);
	}

	private static void testSensorWithCisternSourceValveAffected() throws Exception {
		Cistern tank1 = new Cistern(Double.valueOf(200), 1);
		tank1.addContent(Double.valueOf(300));
		Cistern tank2 = new Cistern(Double.valueOf(150), 2);
		Valve valve = new Valve(tank1, tank2, 1);
		valve.openStep();
		Test.testSensor(tank1);
		valve.openStep();
		Test.testSensor(tank2);
	}

	private static void testSensorWithCisternSourceValveAffectedAndPounding() throws Exception {
		Cistern tank1 = new Cistern(Double.valueOf(200), 1);
		tank1.addContent(Double.valueOf(300));
		Cistern tank2 = new Cistern(Double.valueOf(150), 2);
		Pounding pounding = new Pounding(1);
		Valve valve = new Valve(tank1, tank2, pounding, 1);
		valve.openStep();
		Test.testSensor(tank1);
		Test.testSensor(tank2);
		valve.openStep();
		pounding.activate();
		Test.testSensor(tank2);
		Test.testSensor(tank1);
	}

	private static void testLevelSensorWithCisternSourceValveAffected() throws Exception {
		Cistern tank1 = new Cistern(Double.valueOf(200), 1);
		tank1.addContent(Double.valueOf(300));
		Cistern tank2 = new Cistern(Double.valueOf(150), 2);
		Valve valve = new Valve(tank1, tank2, 1);
		valve.openStep();
		Test.testLevelSensor(tank1, Criteria.LOWER);
		valve.openStep();
		Test.testLevelSensor(tank2, Criteria.HIGHER);
	}

	private static void testEnvironmentSensor() {
		Environment source = new Environment(new SensedValue(Double.valueOf(15)), 1);
		Test.testSensor(source, 25);
	}

	private static void testEnvironmentSensorEnvironmentRegulatorAffected() {
		Environment source = new Environment(new SensedValue(Double.valueOf(15)), 1);
		EnvironmentRegulator regulator = new EnvironmentRegulator(source, 1);
		Test.testSensor(source, 25);
		regulator.reguleValue(Double.valueOf(-20));
		Test.testSensor(source, 300);
	}

	public static void testSensor(Source source) {
		Sensor sensor = new Sensor(source, 200, 1);
		for (int i = 0; i < 25; i++) {
			System.out.println(i + " = " + sensor.getSensedValue().getValue());
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void testSensor(Source source, Integer amount) {
		Sensor sensor = new Sensor(source, 20, 1);
		for (int i = 0; i < amount; i++) {
			System.out.println(i + " = " + sensor.getSensedValue().getValue());
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void testLevelSensor(Source source, Criteria criteria) {
		Sensor sensor = new Sensor(source, 200, 1);
		LevelSensor levelSensor = new LevelSensor(0.5, criteria, sensor, 1);
		for (int i = 0; i < 50; i++) {
			System.out.println(i + " = " + levelSensor.isLevelReached() + " ; " + sensor.getSensedValue().getValue());
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
